/*
 * Java Card File Parser 
 * Based on openjcvm project 
 */
package org.naive.metawatch.converter.parser;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Array;
import java.lang.reflect.Field;

/**
 *
 * @author Andrew Butenko
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ParsedIntArray {

    FieldSize indexSize();

    FieldSize itemSize();

    public class FieldParser {

        public static Object parse(ParsedIntArray pia, ParseContext px, Field f) throws Exception {
            Class<?> componentType = f.getType().getComponentType();
            return parse(px, pia.indexSize(), pia.itemSize(), componentType);
        }

        public static Object parse(ParseContext px, FieldSize indexSize, FieldSize itemSize,
                Class<?> componentType) throws Exception {
            int size = px.readU(indexSize);
            Object a = Array.newInstance(componentType, size);
            for (int i = 0; i < size; i++) {
                Array.set(a, i, px.read(itemSize));
            }
            return a;
        }
    }
}
